


import {
    get,
    post,
    put,
  } from "./base_service"
  
  
  /**
   * 佳宝首页相关接口
   *
   * allen  2023-12-19
   */
  
  /**
   * 
   * 获取购物车数量
   */
  const queryCartCount = () => {
    return new Promise((resolve, reject) => {
      get("/api/front/cart/count?numType=true&type=total").then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.message)
        }
      }).catch((e) => {
        reject(e.message)
      })
    })
  }
 
  /**
   * 
   * 获取购物车列表
   */
  const queryCartList = (page,limit,isValid=true) => {
    return new Promise((resolve, reject) => {
      get("/api/front/cart/list?page=" + page + "&limit=" + limit + "&isValid=" + isValid).then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.message)
        }
      }).catch((e) => {
        reject(e.message)
      })
    })
  }
    /**
   * 
   * 购物车商品数量加减
   * {"id":7,"number":1,}
   */
  const cartGoodNumChange = (data) => {
    return new Promise((resolve, reject) => {
      post("/api/front/cart/num", data,false,true).then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.message)
        }
      }).catch((e) => {
        reject(e.message)
      })
    })
  }

    /**
   * 
   * 购物车删除商品
   * ids：商品id
   */
  const deleteCartGoods = (data) => {
    return new Promise((resolve, reject) => {
      post("/api/front/cart/delete", data,false, true).then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.message)
        }
      }).catch((e) => {
        reject(e.message)
      })
    })
  }
   /**
   * 
   * 添加商品到购物车
   */
   const saveGoodToCart = (data) => {
    return new Promise((resolve, reject) => {
      post("/api/front/cart/save", data).then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.message)
        }
      }).catch((e) => {
        reject(e.message)
      })
    })
  }
  
  module.exports = {
    queryCartCount,
    queryCartList,
    cartGoodNumChange,
    deleteCartGoods,
    saveGoodToCart,//添加商品到购物车
  }
  
